package qwq.zyu.spigotPluginTianguo;

import org.bukkit.Location;
import org.bukkit.plugin.java.JavaPlugin;
import qwq.zyu.spigotPluginTianguo.Command.PlayTianguoCommand;
import qwq.zyu.spigotPluginTianguo.Command.StopTianguoCommand;

import java.util.Objects;

public final class SpigotPluginTianguo extends JavaPlugin {

    private static SpigotPluginTianguo instance;
    private boolean isPlaying = false;
    private long lastPlayTime = 0;

    private Location textPosition;
    private String textDirection;
    private boolean videoEnabled = false;
    private int cooldownSeconds = 0;
    private boolean enableAudio = false;
    private int soundDelayTicks = 0;

    private boolean commandStartEnabled = false;
    private boolean commandStopEnabled = false;

    private boolean clearOnComplete = false;
    private boolean clearOnStop = false;

    private double lineHeight;
    private double scale;
    private double pixelSize;

    private VideoPlayer videoPlayer;

    @Override
    public void onEnable() {
        instance = this;

        saveDefaultConfig();
        loadConfiguration();

        videoPlayer = new VideoPlayer(this);
        getServer().getScheduler().runTaskAsynchronously(this, () -> {
            if (videoPlayer.loadFrames()) {
                getLogger().info("视频帧已成功预加载到内存。");
            } else {
                getLogger().warning("加载视频帧失败！请检查 'video.bin' 文件是否存在且格式正确。");
            }
        });

        Objects.requireNonNull(getCommand("play_tianguo")).setExecutor(new PlayTianguoCommand(this, videoPlayer));
        Objects.requireNonNull(getCommand("stop_tianguo")).setExecutor(new StopTianguoCommand(this, videoPlayer));

        getLogger().info("Tianguo Plugin 已启用！");
    }

    @Override
    public void onDisable() {
        if (videoPlayer != null) {
            videoPlayer.stop();
        }
        instance = null;
        getLogger().info("Tianguo Plugin 已禁用！");
    }

    public static SpigotPluginTianguo getInstance() {
        return instance;
    }

    public void loadConfiguration() {
        reloadConfig();

        double textX = getConfig().getDouble("video_text.position.x", 10.1);
        double textY = getConfig().getDouble("video_text.position.y", -60.2);
        double textZ = getConfig().getDouble("video_text.position.z", 10.3);
        textPosition = new Location(getServer().getWorlds().get(0), textX, textY, textZ);
        textDirection = getConfig().getString("video_text.direction", "NORTH");
        pixelSize = getConfig().getDouble("video_text.pixel_size", 0.0999);

        videoEnabled = getConfig().getBoolean("playback.enabled", true);
        cooldownSeconds = getConfig().getInt("playback.cooldown", 235);
        enableAudio = getConfig().getBoolean("playback.enableAudio", true);
        soundDelayTicks = getConfig().getInt("playback.sound_delay_ticks", 1);

        commandStartEnabled = getConfig().getBoolean("triggers.command_start_enabled", true);
        commandStopEnabled = getConfig().getBoolean("triggers.command_stop_enabled", true);

        clearOnComplete = getConfig().getBoolean("cleanup.clear_on_complete", true);
        clearOnStop = getConfig().getBoolean("cleanup.clear_on_stop", true);

        lineHeight = getConfig().getDouble("video_text.line_height", 0.2);
        scale = getConfig().getDouble("video_text.scale", 1.0);

        getLogger().info("配置已加载:");
        getLogger().info("Text模式 - 墙体位置(" + textX + "," + textY + "," + textZ + "), 朝向: " + textDirection);
        getLogger().info("触发方式配置已加载完成");
        getLogger().info("清理配置 - [完成:" + clearOnComplete + ", 停止:" + clearOnStop + "]");
    }

    public boolean isPlaying() {
        return isPlaying;
    }

    public void setPlaying(boolean playing) {
        this.isPlaying = playing;
        if (playing) {
            this.lastPlayTime = System.currentTimeMillis();
        }
    }

    public boolean canPlay() {
        if (!videoEnabled) return false;
        if (isPlaying) return false;

        long currentTime = System.currentTimeMillis();
        long timePassed = (currentTime - lastPlayTime) / 1000;
        return timePassed >= cooldownSeconds;
    }

    public double getRemainingCooldown() {
        long currentTime = System.currentTimeMillis();
        double timePassed = (currentTime - lastPlayTime) / 1000.0;
        return Math.max(0.0, cooldownSeconds - timePassed);
    }

    public void resetCooldown() {
        this.lastPlayTime = 0;
    }

    public double getLineHeight() {
        return lineHeight;
    }

    public double getScale() {
        return scale;
    }

    // Getters
    public Location getTextPosition() {
        return textPosition;
    }

    public String getTextDirection() {
        return textDirection;
    }

    public VideoPlayer getVideoPlayer() {
        return videoPlayer;
    }

    public int getSoundDelayTicks() {
        return soundDelayTicks;
    }

    public boolean isAudioEnabled() {
        return enableAudio;
    }

    public boolean isCommandStartEnabled() {
        return commandStartEnabled;
    }

    public boolean isCommandStopEnabled() {
        return commandStopEnabled;
    }

    public boolean isClearOnComplete() {
        return clearOnComplete;
    }

    public boolean isClearOnStop() {
        return clearOnStop;
    }

    public double getPixelSize() {
        return pixelSize;
    }
}
